package com.cloud.stock;

import cn.hutool.json.JSONUtil;
import com.cloud.controller.TestController;
import com.cloud.entity.Root;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.util.*;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.cloud.stock
 * @date:2021/9/17
 */
public class 分类4 {
    private static Map<String, Integer> map = new HashMap<String, Integer>() {
        {
            put("1", 1);
            put("2", 2);
            put("3", 3);
            put("4", 4);
            put("5", 5);
            put("6", 6);
            put("7", 7);
            put("8", 8);
            put("9", 9);
            put("0", 0);
            put("a", 11);
            put("b", 12);
            put("c", 13);
            put("d", 14);
            put("e", 15);
            put("f", 16);
            put("g", 17);
            put("h", 18);
            put("i", 19);
            put("j", 20);
            put("k", 21);
            put("l", 22);
            put("m", 23);
            put("n", 24);
            put("o", 25);
            put("p", 26);
            put("q", 27);
            put("r", 28);
            put("s", 29);
            put("t", 30);
            put("u", 31);
            put("v", 32);
            put("w", 33);
            put("x", 34);
            put("y", 35);
            put("z", 36);
            put("A", 11);
            put("B", 12);
            put("C", 13);
            put("D", 14);
            put("E", 15);
            put("F", 16);
            put("G", 17);
            put("H", 18);
            put("I", 19);
            put("J", 20);
            put("K", 21);
            put("L", 22);
            put("M", 23);
            put("N", 24);
            put("O", 25);
            put("P", 26);
            put("Q", 27);
            put("R", 28);
            put("S", 29);
            put("T", 30);
            put("U", 31);
            put("V", 32);
            put("W", 33);
            put("X", 34);
            put("Y", 35);
            put("Z", 36);
            put("-", 37);
            put(":", 38);
            put("*", 39);
            put("+", 40);
            put(".", 41);
            put("$", 42);
        }
    };

    public static void main(String[] args) throws Exception {

        File file = new File("/Users/libin/Desktop/码流转4/");

        File[] fileList = file.listFiles();
        Arrays.sort(fileList, new Comparator<File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }

        });

        for (File thisFile : fileList) {
            System.out.println(thisFile.getName());
            Map<Integer, List> map2 = new HashMap<Integer, List>();
            Scanner scanner = new Scanner(thisFile);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                Root r = JSONUtil.toBean(line, Root.class);
                String barcode = r.get_barcode_content();
                try {
                    if (StringUtils.isNotBlank(barcode)) {
                        Integer last = map.get(barcode.substring(barcode.length() - 1));
                        Integer second = map.get(barcode.substring(barcode.length() - 2,barcode.length()-1));
                        List<String> list = map2.get(last+second);
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        list.add(line+"\r\n");
                        map2.put(last+second, list);
                    }
                }catch (Exception e) {
                    System.out.println(line);
                    continue;
                }
            }

            for (Integer key : map2.keySet()) {
                File f = new File("/Users/libin/Desktop/分类3/" + key);
                TestController.writeFile(f.getAbsolutePath(), map2.get(key));

            }

            thisFile.delete();
        }

    }
}
